package com.yuhang.security.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.mysql.cj.mysqlx.protobuf.MysqlxCrud.Delete;
import com.yuhang.security.dto.User;
import com.yuhang.security.dto.UserQueryCondition;

import io.swagger.annotations.ApiOperation;;
/**
 * 过滤器
 * @author Administrator
 *
 */

@RestController
@RequestMapping(value = "/user")
public class UserController {
	
	@DeleteMapping(value = "/{id:\\d+}")
	public void delete(@PathVariable String id){
		System.out.println("delete"+id);
	}

	@PutMapping(value = "/{id:\\d+}")
	@JsonView(User.UserDetailView.class)
	@ApiOperation(value = "用户更新信息")
	public User updateInfo(@Valid @RequestBody User user,BindingResult errors,@PathVariable(name= "id") String id){

		if (errors.hasErrors()) {
			errors.getAllErrors().stream().forEach(error -> {
				System.out.println(error.getDefaultMessage());	
			}
			);
		}
		
		return user;
	}
	
	@PostMapping
	public User CreatedBy(@Valid @RequestBody User user,BindingResult errors){
		
		if (errors.hasErrors()) {
			errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
		}
		
		System.out.println(ReflectionToStringBuilder.toString(user,ToStringStyle.MULTI_LINE_STYLE));
		user.setId("1");
		return user;
	}
	
	@GetMapping
	@JsonView(User.UserSimpleView.class)
	//public List<User> query(@RequestParam(defaultValue = "yuhang", required = false) String username) {
	public List<User> query(UserQueryCondition userQueryCondition,Pageable pageable) {
		System.out.println(ReflectionToStringBuilder.toString(userQueryCondition,ToStringStyle.MULTI_LINE_STYLE));
		System.out.println(ReflectionToStringBuilder.toString(pageable,ToStringStyle.MULTI_LINE_STYLE));
		List<User> list = new ArrayList<>();
		list.add(new User());
		list.add(new User());
		list.add(new User());
		return list;
	}
	
	@GetMapping(value = "/{id:\\d+}")
	@JsonView(User.UserDetailView.class)
	public User getInfo(@PathVariable(name= "id") String id){
		
		
		//throw new UsernameNotFoundException("id");
		System.out.println("进入getinfo服务");		
		User user = new User();
		user.setUsername("tom");
		return user;
	}

}
